package com.singfan.core.dao.mongo.impl;

import com.mongodb.*;
import com.singfan.common.nosql.mongo.MongoContext;
import com.singfan.common.nosql.mongo.MongoDaoBase;
import com.singfan.core.dao.mongo.IShopLocationDao;

import org.bson.types.ObjectId;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: hansonghe
 * Date: 2015-08-21 10:27
 * Desc:
 */
@Repository
public class ShopLocationDaoImpl extends MongoDaoBase implements IShopLocationDao {
    private static final String COLLECTION_NAME = "shop_hair";

    @Override
    public void createShopLocation(long masterShopId, long shopId, int cityId,
                                   int businessCircleId, List<Integer> serviceTypeList,
                                   double longitude, double latitude) {
        DBObject dbo = new BasicDBObject();
        dbo.put("_id", new ObjectId());
        dbo.put("master_shop_id", masterShopId);
        dbo.put("shop_id", shopId);
        dbo.put("city_id", cityId);
        dbo.put("business_circle_id", businessCircleId);
        Integer[] serviceTypeArr = new Integer[serviceTypeList.size()];
        for (int i = 0; i < serviceTypeList.size(); i++) {
            serviceTypeArr[i] = serviceTypeList.get(i);
        }
        dbo.put("service_type", serviceTypeArr);
        dbo.put("location", new double[] { longitude, latitude });
        insertObj(COLLECTION_NAME, dbo);
    }

    @Override
    public List<Long> findShopNearbyLocation(int serviceType, int cityId, int businessCircleId,
                                             double longitude, double latitude, int pageIndex,
                                             int pageSize) {
        BasicDBObject searchObj = new BasicDBObject();
        if (serviceType > 0) {
            searchObj.put("service_type", serviceType);
        }
        if (businessCircleId == 0) {
            searchObj.put("city_id", cityId);
        } else {
            searchObj.put("business_circle_id", businessCircleId);
        }
        searchObj.put("location", new BasicDBObject("$near", new double[] { longitude, latitude }));
        List<DBObject> shopObjList = selectList(COLLECTION_NAME, searchObj, pageIndex, pageSize);
        List<Long> shopIdList = new ArrayList<>(shopObjList.size());
        for (DBObject shopObj : shopObjList) {
            shopIdList.add(Long.valueOf(shopObj.get("shop_id").toString()));
        }

        return shopIdList;
    }

    @Override
    public List<Long> findShopNearbyLocation(long masterShopId, double longitude, double latitude,
                                             int pageIndex, int pageSize) {
        BasicDBObject searchObj = new BasicDBObject();
        searchObj.put("master_shop_id", masterShopId);
        searchObj.put("location", new BasicDBObject("$near", new double[] { longitude, latitude }));
        List<DBObject> shopObjList = selectList(COLLECTION_NAME, searchObj, pageIndex, pageSize);
        List<Long> shopIdList = new ArrayList<>(shopObjList.size());
        for (DBObject shopObj : shopObjList) {
            shopIdList.add(Long.valueOf(shopObj.get("shop_id").toString()));
        }

        return shopIdList;
    }

    //TODO:临时导数据用，更新门店是否有30、60、90的服务
    @Override
    public void updateShopServices(long shopId, int serviceType) {
        try {
            BasicDBObject searchObj = new BasicDBObject();
            searchObj.put("shop_id", shopId);
            DBObject shopObj = selectList(COLLECTION_NAME, searchObj, 0, 1).get(0);
            BasicDBList basicDBList = (BasicDBList) shopObj.get("service_type");
            List<Integer> serviceTypeList = new ArrayList<>(basicDBList.size());
            for (Object basicDB : basicDBList) {
                serviceTypeList.add((Integer) basicDB);
            }
            if (!serviceTypeList.contains(serviceType)) {
                serviceTypeList.add(serviceType);

                Integer[] newServiceTypeArr = new Integer[serviceTypeList.size()];
                for (int i = 0; i < serviceTypeList.size(); i++) {
                    newServiceTypeArr[i] = serviceTypeList.get(i);
                }

                shopObj.put("service_type", newServiceTypeArr);

                DB db = MongoContext.getMongoClientProvider().getMongoClient()
                    .getDB(getMongoDatabaseName());
                DBCollection collection = db.getCollection(COLLECTION_NAME);
                boolean result = collection.save(shopObj, WriteConcern.SAFE).getN() == 1;
                if (result) {
                    System.out.println(String.format(
                        "add serviceType:%1$d to shop:%2$d successful", serviceType, shopId));
                } else {
                    System.out.println(String.format("add serviceType:%1$d to shop:%2$d failed",
                        serviceType, shopId));
                }
            }
        } catch (Exception e) {
            System.out.println("updateShopServices exception" + shopId + ":" + serviceType);
            e.printStackTrace();
        }
    }

    @Override
    protected String getMongoDatabaseName() {
        return "singfan_location_0";
    }
}
